using System;
using System.Collections.Generic;
using OPTFleet.GeocodeService;
using System.ServiceModel;
using System.Text;

namespace OPTFleet.OPTFleet {
	public class Local {
        private int idlocal;
		private string morada1;
		private string morada2;
		private string cidade;
		private string regiao;
		private string codpostal;
        private Double longitude;
        private Double latitute;

        public int IDLocal
        {
            get { return idlocal; }
            set { idlocal = value; }
        }

        public string Morada1{
            get { return morada1; }
            set { morada1 = value; }
        }

        public string Morada2{
            get { return morada2; }
            set { morada2 = value; }
        }

        public string Cidade{
            get { return cidade; }
            set { cidade = value; }
        }

        public string Regiao{
            get { return regiao; }
            set { regiao = value; }
        }

        public string Codpostal{
            get { return codpostal; }
            set { codpostal = value; }
        }

        public Double Longitude
        {
            get { return longitude; }
            set { longitude = value; }
        }

        public Double Latitude
        {
            get { return latitute; }
            set { latitute = value; }
        }

        public Local(){
            idlocal = 0;
            morada1 = "";
            morada2 = "";
            cidade = "";
            regiao = "";
            codpostal = "";
        }

        public Local(int idlocal,string morada1, string morada2, string cidade, string regiao, string codpostal){
            this.IDLocal = idlocal;
            this.Morada1 = morada1;
            this.Morada2 = morada2;
            this.Cidade = cidade;
            this.Regiao = regiao;
            this.Codpostal = codpostal;
        }

        public List<Local> GetLocaisToGeocode()
        {
            // por aqui uma funcao da base de dados
            return (null);   
        }

   
        public override string ToString()
        {
            return (idlocal + " " + morada1 + " " + morada2 + " " + codpostal + " " + cidade + " " + regiao + " " + "Portugal");
        }

        /*
        private void CoordsToGeography()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "update Customer set SpatialLocation = geography::STGeomFromText('POINT(" + location.Longitude + " " + location.Latitude + ")', 4326) where CustomerID = " + customer.ID;
        }*/

        public static void Geocode(string address)
        {
            string key = "AvKgB2sBq4AKvfduNosfNU9AK5eAKnVFyW_oB8TaDf2DPesdn0QRUkVeZu_ci21m";
            string results = "";
            GeocodeService.GeocodeRequest geocodeRequest = new GeocodeService.GeocodeRequest();

            // Set the credentials using a valid Bing Maps key
            geocodeRequest.Credentials = new GeocodeService.Credentials();
            geocodeRequest.Credentials.ApplicationId = key;

            // Set the full address query
            geocodeRequest.Query = address;

            // Set the options to only return high confidence results 
            ConfidenceFilter[] filters = new ConfidenceFilter[1];
            filters[0] = new ConfidenceFilter();
            filters[0].MinimumConfidence = GeocodeService.Confidence.High;

            // Add the filters to the options
            GeocodeOptions geocodeOptions = new GeocodeOptions();
            geocodeOptions.Filters = filters;
            geocodeRequest.Options = geocodeOptions;

            // Make the geocode request
            GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
            GeocodeResponse geocodeResponse = geocodeService.Geocode(geocodeRequest);

            if (geocodeResponse.Results.Length > 0)
                results = String.Format("Latitude: {0}\nLongitude: {1}",
                  geocodeResponse.Results[0].Locations[0].Latitude,
                  geocodeResponse.Results[0].Locations[0].Longitude);
            else
                results = "No Results Found";
        }

        private void ReverseGeocodePoint(string locationString)
        {
            string results = "";
            string key = "AvKgB2sBq4AKvfduNosfNU9AK5eAKnVFyW_oB8TaDf2DPesdn0QRUkVeZu_ci21m";
            ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();

            // Set the credentials using a valid Bing Maps key
            reverseGeocodeRequest.Credentials = new GeocodeService.Credentials();
            reverseGeocodeRequest.Credentials.ApplicationId = key;

            // Set the point to use to find a matching address
            GeocodeService.Location point = new GeocodeService.Location();
            string[] digits = locationString.Split(',');

            point.Latitude = double.Parse(digits[0].Trim());
            point.Longitude = double.Parse(digits[1].Trim());

            reverseGeocodeRequest.Location = point;

            // Make the reverse geocode request
            GeocodeServiceClient geocodeService = new GeocodeServiceClient();
            GeocodeResponse geocodeResponse = geocodeService.ReverseGeocode(reverseGeocodeRequest);

            if (geocodeResponse.Results.Length > 0)
                results = geocodeResponse.Results[0].DisplayName;
            else
                results = "No Results found";
        }

        public string ToStringPDF()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("----Local-----");
            sb.Append("\nIDLocal: ");
            sb.Append(idlocal);
            sb.Append("\nMorada: ");
            sb.Append(morada1);
            sb.Append("\nMorada: ");
            sb.Append(morada2);
            sb.Append("\nCidade: ");
            sb.Append(cidade);
            sb.Append("\nRegiao: ");
            sb.Append(regiao);
            sb.Append("\nCodigo postal: ");
            sb.Append(codpostal);
            sb.Append("\nLatitude: ");
            sb.Append(latitute);
            sb.Append("\nLongitude: ");
            sb.Append(longitude);
            sb.Append("\n---------------");


            return (sb.ToString());
        }
	}
}
